fun main() {
//    创建一个不可变集合
    val readOnlyShapes: List<String> = listOf("triangle", "banana", "orange")
    println(readOnlyShapes)
//    创建一个可变集合
    val shapes: MutableList<String> = mutableListOf("triangle", "banana", "orange")
    shapes.add("study")
    println(shapes)
    val sum = sum(12, 12)
    println(sum)
    println("------------------------")
    val maxOf1 = maxOf1(12, 13)
    println(maxOf1)
    val maxOf2 = maxOf2(12, 12)
    println(maxOf2)
    val maxOf3 = maxOf3(13, 12)
    println(maxOf3)
    whileOf()
    println()
    val whenOf = whenOf(3)
    println(whenOf)
    whenByList()
    println()
    collectionUsingLambda()
}

fun sum(a: Int, b: Int): Int {
    return a + b
}

fun maxOf1(a: Int, b: Int): Int {
    return if (a > b) a else b
}

fun maxOf2(a: Int, b: Int): Int {
    return if (a > b) {
        a
    } else {
        b
    }
}

fun maxOf3(a: Int, b: Int) = if (a > b) a else b

/*
step1 创建一个装字符串的集合items
step2 创建一个需要再循环里边++的索引变量index
step3 使用while关键字，将index 设置小于items.size
step4 将index++
 */
fun whileOf() {
    val items = listOf("one", "two", "three")
    var index = 0
    while (index < items.size) {
        print(items[index] + "\t")
        index++
    }
}

fun whenOf(obj: Any): String = when (obj) {
    1 -> "one"
    "hello" -> "hi"
    is Long -> "isLong"
    else -> "Unknown"
}

fun whenByList() {
    val items = listOf("apple", "banana", "pineapple")
    /*
    如何使用的是集合或者字典这个when就不可以传实际参数了
     */
    when {
        "apple" in items -> println("when匹配中的apple")
    }
}

fun collectionUsingLambda() {
    val fruits = listOf("apple", "peach", "pineapple")
    fruits
        .filter { it.startsWith("p") }
        .sortedBy { it }
        .map { it.uppercase() }
        .forEach(::println)
}